﻿//********************************************************************************//
//创建日期：<创建日期，2016-4-13>
//页面创建：<孙雪婷，sunxueting@shanghai3h.com>
//后台逻辑：<孙雪婷，sunxueting@shanghai3h.com>2016-06-16
//功能说明：内部转账
//********************************************************************************//
using BLL;
using BLL.Access;
using Entity;
using System;
using System.Data;
using System.Web.UI;
using Web.Class;

namespace Web.Pages.YongShui._6.ZhangWuGL
{
    public partial class NeiBuZZ : BasePage
    {


        #region 属性
        private DataTable dtNeiBuZZ
        {
            get
            {
                return ViewState["dtNeiBuZZ"] as DataTable;
            }
            set
            {
                ViewState["dtNeiBuZZ"] = value;
            }
        }

        /// <summary>
        /// 内部转账信息id
        /// </summary>
        private int NeiBuZZXX_ID
        {
            get
            {
                return Convert.ToInt32(ViewState["NeiBuZZXX_ID"]);
            }
            set
            {
                ViewState["NeiBuZZXX_ID"] = value;
            }
        }
      
        #endregion

        #region 变量
        /// <summary>
        /// 内部转账信息BLL
        /// </summary>
        YS_ZW_NEIBUZZXXBLL _YS_ZW_NEIBUZZXXBLL = new YS_ZW_NEIBUZZXXBLL();
        /// <summary>
        /// 用户信息BLL
        /// </summary>
         YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();
        /// <summary>
        /// 账务信息BLL
        /// </summary>
         YS_ZW_YINGYEZBLL _YS_ZW_YINGYEZBLL = new YS_ZW_YINGYEZBLL();
        /// <summary>
        /// 数据访问操作类
        /// </summary>
         SpHelper _spHelper = new SpHelper();
        /// <summary>
        /// 日志操作类
        /// </summary>
         SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion

        #region 事件
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                 InitData();
             }

             this.dtNeiBuZZ = _YS_ZW_NEIBUZZXXBLL.SelectYS_ZW_NEIBUZZXXesDynamic("i_jlzt=0 and nvl(s_by1,0)=0", "d_chulirq desc");
             this.gvZhuanZhangLB.DataSource = this.dtNeiBuZZ;
             this.gvZhuanZhangLB.DataBind();
             
         }

        /// <summary>
        /// 取消内部转账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuXiaoNBZZ_Click(object sender, EventArgs e)
        {
            DataRow[] dr = this.dtNeiBuZZ.Select("ID=" + this.NeiBuZZXX_ID);
            int bh_out = Convert.ToInt32(dr[0].ItemArray[4]);
            int bh_in = Convert.ToInt32(dr[0].ItemArray[5]);
            int clr = Convert.ToInt32(dr[0].ItemArray[3]);
            decimal je = Convert.ToInt32(dr[0].ItemArray[6]);
            int? result = -1;
            _spHelper.P_YS_ZW_NEIBUZZ(bh_out, bh_in, "", clr, je, this.NeiBuZZXX_ID, out result);
            if (result == 1)
            {
                InitData();
            }
        }

        /// <summary>
        /// 确认转账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQueDing_Click(object sender, EventArgs e)
        {
            bool r_out = false;
            bool r_in = false;
            int bh_out = 0, bh_in = 0;
            if (Validate())
            {
                //先验证是否有加价开账记录
                string yhh_out = txtYongHuH_out2.Text;
                int ym_out = Convert.ToInt32(txtShuiLiangNY_out2.Text);
                DataTable dt_out = _YS_ZW_YINGYEZBLL.SelectYS_ZW_YINGYEZsDynamic(string.Format("S_YONGHUH='{0}' AND I_Y*100+I_M={1}", yhh_out, ym_out), null);
                //验证通过后，将该用户号对应的消息显示在输入框内
                if (dt_out != null && dt_out.Rows.Count > 0)
                {
                    decimal ysje_out = Convert.ToDecimal(dt_out.Rows[0]["N_YINGSHOUJE"]);
                    decimal ssje_out = Convert.ToDecimal(dt_out.Rows[0]["N_SHISHOUJE"]);
                    bh_out = Convert.ToInt32(dt_out.Rows[0]["I_FEIYONGBH"]);
                    DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_out), null);
                    string yhm_out = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                    txtYongHuM_out2.Text = yhm_out;
                    txtYingShouJE_out2.Text = ysje_out.ToString();
                    txtShiShouJE_out2.Text = ssje_out.ToString();
                    r_out = true;
                }
                else
                {
                    PageTools.AlertMsg(this, "您输入的转出费用信息没有找到加价开账记录");
                }
                string yhh_in = txtYongHuH_in2.Text;
                int ym_in = Convert.ToInt32(txtShuiLiangNY_in2.Text);
                DataTable dt_in = _YS_ZW_YINGYEZBLL.SelectYS_ZW_YINGYEZsDynamic(string.Format("S_YONGHUH='{0}' AND I_Y*100+I_M={1}", yhh_in, ym_in), null);
                if (dt_in != null && dt_in.Rows.Count > 0)
                {
                    decimal ysje_in = Convert.ToDecimal(dt_in.Rows[0]["N_YINGSHOUJE"]);
                    decimal ssje_in = Convert.ToDecimal(dt_in.Rows[0]["N_SHISHOUJE"]);
                    DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_in), null);
                    bh_in = Convert.ToInt32(dt_in.Rows[0]["I_FEIYONGBH"]);
                    string yhm_in = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                    txtYongHuM_in2.Text = yhm_in;
                    txtYingShouJE_in2.Text = ysje_in.ToString();
                    txtShiShouJE_in2.Text = ssje_in.ToString();
                    r_in = true;
                }
                else
                {
                    PageTools.AlertMsg(this, "您输入的转入费用信息没有找到加价开账记录");
                }
            }
            else
            {
                PageTools.AlertMsg(this, "请先正确输入");
            }
            //显示完后，再执行确认转账的操作
            if (r_out && r_in)
            {
                if (txtZhuanZhangJE.Text.Trim() == "")
                {
                    PageTools.AlertMsg(this, "转账金额不能为空，请先输入");
                }
                else if (Convert.ToDecimal(txtZhuanZhangJE.Text.Trim()) > Convert.ToDecimal(txtShiShouJE_out2.Text.Trim()))
                {
                    PageTools.AlertMsg(this, "转账金额不能大于实收金额");
                }
                else
                {
                    int userid = LoginInfo.CurrentUser.UserID;
                    int? result = -1;
                    decimal zzje = Convert.ToDecimal(txtZhuanZhangJE.Text);
                    _spHelper.P_YS_ZW_NEIBUZZ(bh_out, bh_in, "", userid, zzje, 0, out result);
                    if (result == 1)
                    {
                        PageTools.AlertMsg(this, "确认转账成功");
                    }
                    else
                    {
                        PageTools.AlertMsg(this, "转账失败");
                    }
                }
            }
        }

        /// <summary>
        /// 转账信息列表，行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvZhuanZhangLB_FocusedRowChanged(object sender, EventArgs e)
        {
            if (this.dtNeiBuZZ != null && this.dtNeiBuZZ.Rows.Count > 0)
            {
                this.NeiBuZZXX_ID = Convert.ToInt32(this.gvZhuanZhangLB.GetRowValues(this.gvZhuanZhangLB.FocusedRowIndex, "ID"));
                SetDataTab1();
            }
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }
        #endregion

        #region 方法

        /// <summary>
        /// 页面加载
        /// </summary>
        private void InitData()
        {
            //加载列表信息
            this.dtNeiBuZZ = _YS_ZW_NEIBUZZXXBLL.SelectYS_ZW_NEIBUZZXXesDynamic("i_jlzt=0 and nvl(s_by1,0)=0", "d_chulirq desc");
            this.gvZhuanZhangLB.DataSource = this.dtNeiBuZZ;
            this.gvZhuanZhangLB.DataBind();
            if (this.dtNeiBuZZ != null && this.dtNeiBuZZ.Rows.Count > 0)
            {
                this.gvZhuanZhangLB.FocusedRowIndex = 0;
                SetDataTab1();
            }
            deZhuanZhangRQ.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 加载列表下方输入框信息
        /// </summary>
        private void SetDataTab1()
        {
            DataRow[] dr = this.dtNeiBuZZ.Select("ID=" + this.NeiBuZZXX_ID);
            if (dr.Length > 0)
            {
                //转出费用信息
                int bh_out = Convert.ToInt32(dr[0].ItemArray[4]);
                DataTable dt_out = _YS_ZW_YINGYEZBLL.SelectYS_ZW_YINGYEZsDynamic(string.Format("I_FEIYONGBH={0}", bh_out), null);
                if (dt_out != null && dt_out.Rows.Count > 0)
                {
                    string yhh_out = dt_out.Rows[0]["S_YONGHUH"].ToString();
                    decimal ysje_out = Convert.ToDecimal(dt_out.Rows[0]["N_YINGSHOUJE"]);
                    decimal ssje_out = Convert.ToDecimal(dt_out.Rows[0]["N_SHISHOUJE"]);
                    int ym_out = Convert.ToInt32(dt_out.Rows[0]["I_Y"]) * 100 + Convert.ToInt32(dt_out.Rows[0]["I_M"]);
                    DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_out), null);
                    string yhm_out = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                    txtYongHuH_out.Text = yhh_out;
                    txtYongHuM_out.Text = yhm_out;
                    txtShuiLiangNY_out.Text = ym_out.ToString();
                    txtYingShouJE_out.Text = ysje_out.ToString();
                    txtShiShouJE_out.Text = ssje_out.ToString();
                }

                //转入费用信息
                int bh_in = Convert.ToInt32(dr[0].ItemArray[5]);
                DataTable dt_in = _YS_ZW_YINGYEZBLL.SelectYS_ZW_YINGYEZsDynamic(string.Format("I_FEIYONGBH={0}", bh_in), null);
                if (dt_in != null && dt_in.Rows.Count > 0)
                {
                    string yhh_in = dt_in.Rows[0]["S_YONGHUH"].ToString();
                    decimal ysje_in = Convert.ToDecimal(dt_in.Rows[0]["N_YINGSHOUJE"]);
                    decimal ssje_in = Convert.ToDecimal(dt_in.Rows[0]["N_SHISHOUJE"]);
                    int ym_in = Convert.ToInt32(dt_in.Rows[0]["I_Y"]) * 100 + Convert.ToInt32(dt_in.Rows[0]["I_M"]);
                    DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh_in), null);
                    string yhm_in = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                    txtYongHuH_in.Text = yhh_in;
                    txtYongHuM_in.Text = yhm_in;
                    txtShuiLiangNY_in.Text = ym_in.ToString();
                    txtYingShouJE_in.Text = ysje_in.ToString();
                    txtShiShouJE_in.Text = ssje_in.ToString();
                }
            }
        }

        /// <summary>
        /// 验证是否输入框都显示不为空
        /// </summary>
        /// <returns></returns>
        private bool Validate()
        {
            bool res = true;
            if (txtYongHuH_out2.Text.Trim() == "")
            {
                res = false;
            }
            if (txtShuiLiangNY_out2.Text.Trim() == "")
            {
                res = false;
            }
            if (txtYongHuH_in2.Text.Trim() == "")
            {
                res = false;
            }
            if (txtShuiLiangNY_in2.Text.Trim() == "")
            {
                res = false;
            }

            return res;
        }

        
        #endregion

        /// <summary>
        /// 前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int result = -1;
            string msg = "";

            string yhh = this.hdCollection.Get("yhh").ToString();
            int ym = Convert.ToInt32(this.hdCollection.Get("ym"));
            if (e.Parameter == "setinfo_out")
            {
                //先验证是否有加价开账记录
                DataTable dt_out = _YS_ZW_YINGYEZBLL.SelectYS_ZW_YINGYEZsDynamic(string.Format("S_YONGHUH='{0}' AND I_Y*100+I_M={1}", yhh, ym), null);
                //验证通过后，将该用户号对应的消息显示在输入框内
                if (dt_out != null && dt_out.Rows.Count > 0)
                {
                    result = 1;
                }
                if (result ==1)
                {
                    decimal ysje_out = Convert.ToDecimal(dt_out.Rows[0]["N_YINGSHOUJE"]);
                    decimal ssje_out = Convert.ToDecimal(dt_out.Rows[0]["N_SHISHOUJE"]);
                    int bh_out = Convert.ToInt32(dt_out.Rows[0]["I_FEIYONGBH"]);
                    DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh), null);
                    string yhm_out = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                    int r_out = 1;
                    msg = string.Join("|", Convert.ToString(result), Convert.ToString(yhm_out), Convert.ToString(ysje_out), Convert.ToString(ssje_out), Convert.ToString(r_out), Convert.ToString(bh_out));
                }
                else
                {
                    msg = string.Join("|", Convert.ToString(result));
                }

            }
            else if (e.Parameter == "setinfo_in")
            {
                //先验证是否有加价开账记录 
                DataTable dt_in = _YS_ZW_YINGYEZBLL.SelectYS_ZW_YINGYEZsDynamic(string.Format("S_YONGHUH='{0}' AND I_Y*100+I_M={1}", yhh, ym), null);
                //验证通过后，将该用户号对应的消息显示在输入框内
                if (dt_in != null && dt_in.Rows.Count > 0)
                {
                    result = 1;
                }
                if (result==1)
                {
                    decimal ysje_in = Convert.ToDecimal(dt_in.Rows[0]["N_YINGSHOUJE"]);
                    decimal ssje_in = Convert.ToDecimal(dt_in.Rows[0]["N_SHISHOUJE"]);
                    DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}'", yhh), null);
                    int bh_in = Convert.ToInt32(dt_in.Rows[0]["I_FEIYONGBH"]);
                    string yhm_in = dt != null && dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                    //txtYongHuM_in2.Text = yhm_in;
                    //txtYingShouJE_in2.Text = ysje_in.ToString();
                    //txtShiShouJE_in2.Text = ssje_in.ToString();
                    int r_in = 1;
                    msg = string.Join("|", Convert.ToString(result), Convert.ToString(yhm_in), Convert.ToString(ysje_in), Convert.ToString(ssje_in), Convert.ToString(r_in), Convert.ToString(bh_in));
                }
                else
                {
                    msg = string.Join("|", Convert.ToString(result));
                }

            }
            else if (e.Parameter == "qdzz")
            {
                int userid = LoginInfo.CurrentUser.UserID;
                int? result2 = -1;
                decimal zzje = Convert.ToDecimal(txtZhuanZhangJE.Text);
                _spHelper.P_YS_ZW_NEIBUZZ(Convert.ToInt32(yhh), ym, "", userid, zzje, 0, out result2);
                msg = string.Join("|", Convert.ToString(result2));
            }

             else if (e.Parameter == "qxnbzz")
            {
                DataRow[] dr = this.dtNeiBuZZ.Select("ID=" + yhh);
                int bh_out = Convert.ToInt32(dr[0].ItemArray[4]);
                int bh_in = Convert.ToInt32(dr[0].ItemArray[5]);
                int clr = Convert.ToInt32(dr[0].ItemArray[3]);
                decimal je = Convert.ToInt32(dr[0].ItemArray[6]);
                int? result2 = -1;
                _spHelper.P_YS_ZW_NEIBUZZ(bh_out, bh_in, "", clr, je, this.NeiBuZZXX_ID, out result2);
                msg = string.Join("|", Convert.ToString(result2));
            }
            e.Result = e.Parameter + "|0|" + msg;
        }

        protected void gvZhuanZhangLB_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            InitData();
        }

        


    }
}